﻿using System;
using System.Windows.Forms;


namespace Autorun
{
	public partial class ConnectDialog : Form
	{
		#region "Constructors"
		public ConnectDialog()
		{
			InitializeComponent();
		}
		#endregion

		#region "Form Event Handlers"
		private void ConnectDialog_Load(object sender, EventArgs e)
		{
			txtPort.Text = ServerManagement.Instance.Configuration.RemotePort.ToString();
			cbChannel.Text = ServerManagement.Instance.Configuration.RemoteChannel;
		}
		#endregion

		#region "Component Event Handlers"
		private void optInternalExternal_CheckedChanged(object sender, EventArgs e)
		{
			bool external;

			external = optExternal.Checked;

			lblHost.Enabled = external;
			txtHost.Enabled = external;
			lblPort.Enabled = external;
			txtPort.Enabled = external;
			lblChannel.Enabled = external;
			cbChannel.Enabled = external;
			chkAddToServers.Enabled = external;
		}

		private void btOK_Click(object sender, EventArgs e)
		{
			bool hasErrors;
			int i;

			myErrors.Clear();
			hasErrors = false;

			if (optExternal.Checked == true)
			{
				if (txtHost.Text.Length == 0)
				{
					myErrors.SetError(txtHost, "Invalid host name");
					hasErrors = true;
				}

				if ((txtPort.Text.Length == 0) || (int.TryParse(txtPort.Text, out i) == false))
				{
					myErrors.SetError(txtPort, "Invalid port number");
					hasErrors = true;
				}

				if (cbChannel.SelectedIndex == -1)
				{
					myErrors.SetError(cbChannel, "Select a channel");
					hasErrors = true;
				}
			}

			if (hasErrors == false) DialogResult = DialogResult.OK;
		}
		#endregion

		#region "Properties"
		public bool BuiltIn
		{
			get { return optInternal.Checked; }
		}

		public string Host
		{
			get { return optInternal.Checked == true ? null : txtHost.Text; }
		}

		public int Port
		{
			get { return optInternal.Checked == true ? 0 : int.Parse(txtPort.Text); }
		}

		public string Channel
		{
			get { return optInternal.Checked == true ? null : cbChannel.Text; }
		}

		public bool Register
		{
			get { return optInternal.Checked == true ? false : chkAddToServers.Checked; }
		}
		#endregion

	}
}
